Skip to content

Add source names (via new Stream and SourceSpan classes) and .span() combinator (take 2) #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

tsani
Copy link

@tsani tsani commented Jan 23, 2025

Following up from #83, here's a new PR:

  • without any CI adjustments
  • using str and bytes subclasses equipped with a source attribute, wrapping the user's input only when a source is provided to .parse() or .parse_partial().

The test suite is virtually unchanged this time around. I took care to keep everything as backwards compatible as possible, including keeping line_info returning a 2-tuple when no source is given.

tsani added 4 commits January 23, 2025 08:15
For backwards compatibility, these are only used to wrap input data
when a source is given to `parse()` or `parse_partial()`.

When a source is given, the following behaviours change:
- the primitive `line_info` parser returns a 3-tuple instead of a
  2-tuple
- ParseError objects will include the source
@tsani tsani force-pushed the feature/source-span-stream-nonbreaking branch from be70346 to 5c5b293 Compare January 23, 2025 13:15
@tsani
Copy link
Author

tsani commented Feb 2, 2025

Hi @spookylukey, I need your help getting this merged -- can you let me know when you'll be able to check out the PR?

Copy link
Member

@spookylukey spookylukey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really good, thanks for the PR.

In addition to the comment above, please could you also:

  • add a test that covers make_stream() being passed bytes. Currently if I comment out or break those lines, no test fails.
  • for completeness, a test covering the exception in make_stream() as well.
  • add an entry into history.rst, mentioning all the additions,
    plus the change in behaviour for line_info_at() if the new source parameters is passed.

Thanks!

Luke

Comment on lines +35 to +51
def make_stream(data: str | bytes, source: Any):
"""Constructs an appropriate stream type for `data` when it's one of the
three core supported datatypes of parsy (viz. str, bytes, list). Otherwise,
the data is assumed to just support a minimum of __getitem__ and
__len__."""
if isinstance(data, str):
return StrStream(data, source)

if isinstance(data, bytes):
return ByteStream(data, source)

raise RuntimeError(
"A Parsy stream can be formed only on str and bytes, but the given "
f"data has type {type(data)}. If you are separately tokenizing the "
"data to parse, consider instead equipping the tokens with source "
"location metadata.",
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like it handles list, in contrast to its docstring. That's OK, but the docstring should be corrected, and preferably the limitation should be noted in the appropriate docs i.e. the parse method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants